<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/03/0003
 * Time: 14:10
 */

namespace app\sports_admin\model;


use think\Model;
use traits\model\SoftDelete;

class School extends Model
{
    use SoftDelete;
    protected $table = 'school';

    protected $pk = 'school_id';

    protected $createTime = 'school_time';

    protected $autoWriteTimestamp = true;

    protected $hidden = ['password','update_time','create_time','delete_time'];

    public function getEndAttr($value)
    {
        return date('Y-m-d',$value);
    }
    public function getStartAttr($value)
    {
        return date('Y-m-d',$value);
    }
    //获取省名称
    public function province()
    {
        return $this->belongsTo('region','province','id');
    }
    //获取市名称
    public function city()
    {
        return $this->belongsTo('region','city','id');
    }
    //获取县名称
    public function county()
    {
        return $this->belongsTo('region','district','id')->field('id,region_name');
    }
    /**
     * 学校列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     */
    public static function getSchoolInfo($page,$rows,$sort,$sortOrder,$searchName)
    {
        $result = self::with(['province'=>function($query){$query->field('id,region_name');},
            'city'=>function($query){$query->field('id,region_name');},'county'=>function($query){
                $query->field('id,region_name');
            }])->where(function($query)use($searchName){
            if($searchName){
                $query->where('phone_name|school_name','like',"%{$searchName}%");
            }
        })->order("{$sort}","{$sortOrder}")->page($page)->limit($rows)->select();
        $count = self::with(['province'=>function($query){$query->field('id,region_name');},
            'city'=>function($query){$query->field('id,region_name');},'county'=>function($query){
                $query->field('id,region_name');
            }])->where(function($query)use($searchName){
            if($searchName){
                $query->where('phone_name|school_name','like',"%{$searchName}%");
            }
        })->count();
        return json(['total'=>$count,'rows'=>$result]);
    }
    /**
     * 添加学校
     * @param $schoolName 学校名称
     * @param $phone 联系方式(登录账号)
     * @param $password 登录密码
     * @param $url 学校官网
     * @param $email 电子邮件
     * @param $address 地址
     * @param $province 省
     * @param $city 市
    */
    public static function schoolInsert($params)
    {
        return self::create($params);
    }
    /**
     * 代理人添加学校
     * $data
    */
    public static function customerSchoolInsert($data)
    {
        return self::create($data);
    }
    /**
     * 学校信息
     * @param $id 学校id
     */
    public static function schoolInfo($id)
    {
        return self::with(['province'=>function($query){$query->field('id,region_name');},
            'city'=>function($query){$query->field('id,region_name');},'county'=>function($query){
                $query->field('id,region_name');
            }])->where('school_id',$id)->find();
    }
}